<?php
namespace api\home\controller;

use think\Db;
use think\Validate;
use cmf\controller\RestBaseController;
use app\common\model\UserModel;
use app\common\model\AppUserModel;
use app\common\logic\home\Account;
//登录
class AccountController extends RestBaseController
{
	public function checkPhone($phone){
		$data['phone'] = $phone;
		$rule = [
			'phone'=>'require|number|length:11',
		];
		$msg = [
			'phone.require' => '手机号不能为空',
			'phone.number'=>'手机号必须为数字',
			'phone.length'=>'手机号长度必须为11位',
		];
		$validate = new Validate($rule,$msg);
		$result   = $validate->check($data);
		if(!$result){
			$this->error($validate->getError());
		}
		if(!preg_match('/^1[345678]\d{9}$/', $phone)){
			$this->error('请输入正确的手机格式!');
		}
	}
	/**
	 * [检查手机号是否注册]
	 * @return [type] [description]
	 */
	public function chePhone(){
		$param = $this->request->param();
		$data = AppUserModel::where('phone',$param['phone'])->find();
		if(empty($data)){
			$this->error('请注册');
		}else{
			$this->success('请登录');
		}
	}
	/**
	 * [短信登录]
	 * @param [type] $[phone] [手机号]
	 * @param [type] $[code] [验证码]
	 * @return [type] [description]
	 */
	public function loginPhone(Account $Account){
		$param = $this->request->param();
		//字段验证
		$result = $this->validate($param,'AppUser.loginPhone');
		if(true !== $result){
		    // 验证失败 输出错误信息
		    $this->error($result);
		}
		//手机号格式验证
		$this->checkPhone($param['phone']);
		//验证码验证
		$rs = cmf_check_verification_code($param['phone'],$param['code']);
        if($rs){
        	$this->error('验证码错误');
        }
        //手机号登录
        $data = AppUserModel::where('phone',$param['phone'])->find();

        if(!empty($data)){
			//判断注册进度
			$rs = $Account->type($data);
			if($rs['code'] > 0){
				$data['token']  = cmf_generate_user_token($rs['id'],$this->deviceType);
				$data['role'] = $rs['role'];
				$data['code'] = $rs['code'];
				db("app_user")->where(['id'=>$rs['id']])->setInc("use_num");
				db("app_user_log")->insert(['userId'=>$rs['id'],'createTime'=>date('Y-m-d H:i:s')]);
				$this->success(['code'=>$rs['code'],'msg'=>$rs['msg']],$data);
			}else{
			
				$this->error(['code'=>$rs['code'],'msg'=>$rs['msg']],$data['id']);
			}
		
		}else{
			$this->error(['code'=>-1000,'msg'=>'请设置密码']);
		}

	}
	/**
	 * [密码登录]
	 * @param [type] $[phone] [手机号]
	 * @param [type] $[password] [密码]
	 * @return [type] [description]
	 */
	public function loginPassword(Account $Account){
		$param = $this->request->param();
		//字段验证
		$result = $this->validate($param,'AppUser.loginPassword');
		if(true !== $result){
		    // 验证失败 输出错误信息
		    $this->error($result);
		}
		//手机号格式验证
		$this->checkPhone($param['phone']);
		//密码登录
		$data = AppUserModel::where('phone',$param['phone'])->where('password',sha1($param['password']))->find();

		if(!empty($data)){
			//判断注册进度
			$rs = $Account->type($data);
			if($rs['code'] > 0){
				$data['token']  = cmf_generate_user_token($rs['id'],$this->deviceType);
				$data['role'] = $rs['role'];
				$data['code'] = $rs['code'];
				db("app_user")->where(['id'=>$rs['id']])->setInc("use_num");
				db("app_user_log")->insert(['userId'=>$rs['id'],'createTime'=>date('Y-m-d H:i:s')]);
				$this->success(['code'=>$rs['code'],'msg'=>$rs['msg']],$data);
			}else{
			
				$this->error(['code'=>$rs['code'],'msg'=>$rs['msg']],$data['id']);
			}
		
		}else{
			$this->error('账号或密码不正确');
		}

	}
	
	/**
	 * [设置密码]
	 * @param [type] $[phone] [手机号]
	 * @param [type] $[password] [密码]
	 * @return [type] [description]
	 */
	public function setPassword(Account $Account){
		$param = $this->request->param();
		$result = $this->validate($param,'AppUser.loginPassword');
		if(true !== $result){
		    // 验证失败 输出错误信息
		    $this->error($result);
		}
		$rs = $Account->adds($param);
		$this->success('成功',$rs);
	}

	/**
	 * [选择身份]
	 * @param [type] $param.uid [uid]
	 * @param [type] $param.user_type [1 admin-老师 2用户-学生]
	 */
	public function selectTypes(Account $Account){
		$param = $this->request->param();
		$rs = $Account->selectTypes($param);
		if($rs['code'] == 1){
			$data['token']  = cmf_generate_user_token($rs['uid'],$this->deviceType);
			$data['role'] = $rs['role'];
			$this->success('请完善信息',$data);
		}else{
			$this->error(['code'=>$rs['code'],'msg'=>$rs['msg']]);
		}
	}
	/**
	 * [微信绑定手机号]
	 * @param [type] $param.unionid [用户unionid]
	 * @param [type] $param.phone [用户手机号]
	 * @param [type] $param.code [用户验证码]
	 */
	public function bidding(Account $Account){
		$param = $this->request->param();
		$rs = $Account->bidding($param);
		//如果绑定成功
		if($rs['code'] == 1){
			$data = AppUserModel::get($rs['uid']);
			$rs = $Account->type($data);
			if($rs['code'] > 0){
				$data['token']  = cmf_generate_user_token($rs['id'],$this->deviceType);
				$data['role'] = $rs['role'];
				$data['code'] = $rs['code'];
				db("app_user")->where(['id'=>$rs['id']])->setInc("use_num");
				db("app_user_log")->insert(['userId'=>$rs['id'],'createTime'=>date('Y-m-d H:i:s')]);
				$this->success(['code'=>$rs['code'],'msg'=>$rs['msg']],$data);
			}else{
				$this->error(['code'=>$rs['code'],'msg'=>$rs['msg']],$data['id']);
			}
		}else{
			$this->error(['code'=>$rs['code'],'msg'=>$rs['msg']]);
		}
	}
	/**
	 * [微信设置密码]
	 * @param [type] $[phone] [手机号]
	 * @param [type] $[password] [密码]
	 * @param [type] $[unionid] [unionid]
	 * @return [type] [description]
	 */
	public function setPasswordWx(Account $Account){
		$param = $this->request->param();
		$result = $this->validate($param,'AppUser.loginPassword');
		if(true !== $result){
		    // 验证失败 输出错误信息
		    $this->error($result);
		}
		$rs = $Account->addsWx($param);
		if($rs['code'] == 1){
			$this->success('成功',$rs['uid']);
		}else{
			$this->error('失败');
		}
		
	}
	/**
	 * [获取微信信息]
	 * @param [type] $param.unionid [用户unionid]
	 * @param [type] $param.openid [用户openid]
	 * @param [type] $param.nickname [用户昵称]
	 * @param [type] $param.headimgurl [用户头像]
	 * @param [type] $param.sex [性别]
	 * @param [type] $param.more [全部获得的信息]
	 */
	public function add(Account $Account){
		$param = $this->request->param();
		$data = UserModel::get(['unionid'=>$param['unionid']]);
		if(empty($data)){
			$rs = $Account->add($param);
			if($rs){
				$this->error(['code'=>-1001,'msg'=>'请绑定手机号']);
			}else{
				$this->error('失败');
			}
		}else{
			if(empty($data['app_id'])){
				$this->error(['code'=>-1001,'msg'=>'请绑定手机号']);
			}else{
				$data = AppUserModel::get($data['app_id']);
				$rs = $Account->type($data);
				if($rs['code'] > 0){
					$data['token']  = cmf_generate_user_token($rs['id'],$this->deviceType);
					$data['role'] = $rs['role'];
					$data['code'] = $rs['code'];
					db("app_user")->where(['id'=>$rs['id']])->setInc("use_num");
					db("app_user_log")->insert(['userId'=>$rs['id'],'createTime'=>date('Y-m-d H:i:s')]);
					$this->success(['code'=>$rs['code'],'msg'=>$rs['msg']],$data);
				}else{
					$this->error(['code'=>$rs['code'],'msg'=>$rs['msg']],$data['id']);
				}
			}
		}
	}

	/**
	 * [验证找回密码]
	 * @param [type] $[phone] [手机号]
	 * @param [type] $[code] [验证码]
	 * @return [type] [description]
	 */
	public function loginFind(Account $Account){
		$param = $this->request->param();
		//字段验证
		$result = $this->validate($param,'AppUser.loginPhone');
		if(true !== $result){
		    // 验证失败 输出错误信息
		    $this->error($result);
		}
		//手机号格式验证
		$this->checkPhone($param['phone']);
		//验证码验证
		$rs = cmf_check_verification_code($param['phone'],$param['code']);
        if($rs){
        	$this->error('验证码错误');
        }
        //手机号登录
        $data = AppUserModel::where('phone',$param['phone'])->find();

        if(!empty($data)){
        	$this->success('验证成功',$data['id']);
		}else{
			$this->error('验证失败');
		}

	}
	/**
	 * [重置密码]
	 * @param [type] $[phone] [手机号]
	 * @param [type] $[password] [密码]
	 * @return [type] [description]
	 */
	public function setPasswordFind(Account $Account){
		$param = $this->request->param();
		$result = $this->validate($param,'AppUser.loginPassword');
		if(true !== $result){
		    // 验证失败 输出错误信息
		    $this->error($result);
		}
		$rs = AppUserModel::where('phone',$param['phone'])->update(['password'=>sha1($param['password'])]);
		$this->success('成功',$rs);
	}

	





























	/**
	 * [选择身份]
	 * @param [type] $param.unionid [用户unionid]
	 * @param [type] $param.user_type [1 admin-老师 2用户-学生]
	 */
	public function selectType已废弃(Account $Account){
		$param = $this->request->param();
		$rs = $Account->selectType($param);
		if($rs['code']){
			$data['token']  = cmf_generate_user_token($rs['app_id'],$this->deviceType);
			$data['role'] = $rs['role'];
			$this->success('请完善信息',$data);
		}else{
			$this->error('选择身份失败');
		}
	}
}
